﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Dapper_Core.Web.Models;
using Dapper_Core.BLL.Interface;
using Dapper_Core.BLL.Impl;
using Dapper_Core.Model;
using Newtonsoft.Json;
using Dapper_Core.Common;
using Dapper_Core.DAL.Impl;
using Dapper_Core.Table;

namespace Dapper_Core.Web.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            #region 测试IsAssignableFrom的用法
            //Type interfaceType = typeof(IPayService);
            //Type instanceType = typeof(WxpayService);
            ////是否可以将指定类型的实例分配给当前类型对象
            //bool flag1 = interfaceType.IsAssignableFrom(instanceType);//true
            //bool flag2 = instanceType.IsAssignableFrom(interfaceType);//false
            //bool flag3 = interfaceType.IsAssignableFrom(interfaceType);//true
            //bool flag4 = instanceType.IsAssignableFrom(instanceType);//true 
            #endregion

            #region 读写Cookie
            //string one = Request.Cookies["one"];
            //string two = Request.Cookies["two"];
            //string three = Request.Cookies["three"];
            //if (string.IsNullOrEmpty(one) && string.IsNullOrEmpty(two) && string.IsNullOrEmpty(three))
            //{
            //    Response.Cookies.Append("one", "oneValue", new Microsoft.AspNetCore.Http.CookieOptions() { Expires = DateTimeOffset.Now.AddMinutes(1) });
            //    Response.Cookies.Append("two", "twoValue", new Microsoft.AspNetCore.Http.CookieOptions() { Expires = DateTimeOffset.Now.AddMinutes(1) });
            //    Response.Cookies.Append("three", "threeValue", new Microsoft.AspNetCore.Http.CookieOptions() { Expires = DateTimeOffset.Now.AddMinutes(2) });
            //} 
            #endregion

            #region 测试BaseRepositoryExt中的扩展方法
            BaseRepository<Sys_Log> repository = new BaseRepository<Sys_Log>();
            List<Sys_Log> logList = repository.Query("select * from Sys_Log where CreateDate>'2020-09-30 10:30'", null).ToList();
            Sys_Log log = repository.QueryModel("select * from Sys_Log where CreateDate>'2020-09-30 10:30'", null);
            //List<Manufacturer_Info> manufacturerList = repository.Query<Sys_Log, Manufacturer_Info>("select * from Manufacturer_Info;", null).ToList();
            //Manufacturer_Info manufacturer = repository.QueryModel<Sys_Log, Manufacturer_Info>("select * from Manufacturer_Info;", null); 
            bool success = repository.Execute("update Sys_Log set IP='127.0.0.1' where CreateDate>'2020-09-30 14:53:46.100';", null);
            object value = repository.ExecuteScalar("select top 1 Id from Sys_Log where CreateDate>'2020-09-30 14:53:46.100' order by Id asc;", null);
            #endregion

            #region 测试Dapper
            //BaseRepository<Sys_User> userRepo = new BaseRepository<Sys_User>();
            //List<Sys_User> userList01 = userRepo.GetList(item => item.Name.StartsWith("%wangzhen 15390")).ToList();
            //List<Sys_User> userList02 = userRepo.GetList(item => item.Name.StartsWith("'zhaoqiuyu 18982")).ToList();
            //List<Sys_User> userList03 = userRepo.GetList(item => item.Name == "%wangzhen 15390").ToList();
            //List<Sys_User> userList04 = userRepo.GetList(item => item.Name == "'zhaoqiuyu 18982").ToList();

            //BaseRepository<Sys_User> mysqlUserRepo = new BaseRepository<Sys_User>(DBEnum.MySqlADMP);
            //List<Sys_User> mysqlUserList01 = mysqlUserRepo.GetList(item => item.Name.StartsWith("%wangzhen 15390")).ToList();
            //List<Sys_User> mysqlUserList02 = mysqlUserRepo.GetList(item => item.Name.StartsWith("'zhaoqiuyu 18982")).ToList();
            //List<Sys_User> mysqlUserList03 = mysqlUserRepo.GetList(item => item.Name == "%wangzhen 15390").ToList();
            //List<Sys_User> mysqlUserList04 = mysqlUserRepo.GetList(item => item.Name == "'zhaoqiuyu 18982").ToList();
            #endregion

            return View();
        }

        public IActionResult Logout()
        {
            SessionUtility.ClearSession();
            return RedirectToAction("Index", "Login");

            //string logoutPage = AppsettingsUtility.SSOConfig.SSOLogoutUrl;
            //HttpContext httpContext = MyHttpContext.Current; 
            //string test = SSOHttpFilter4Net.SSOLogoutNew(httpContext, logoutPage);
            ////WebUtility.ToLoginPage();
            //bool istes = SSOHttpFilter4Net.SSOLogoutNew(httpContext);

            ////Microsoft.AspNetCore.Mvc.RazorPages.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>javascript:window.open('" + logoutPage + "',target='mframe');</script>");
            ////return RedirectToAction("Index", "Login");
            ////
            //string url = $"https://sso.h3c.com/LogoutSSO.aspx?siteId={AppsettingsUtility.SSOConfig.SiteID}&RequestUrl={AppsettingsUtility.SSOConfig.LogonUrl}";
            //return Redirect(url);
        }

        public IActionResult GetSideMenus()
        {
            List<SideMenuTreeNode> treeNodes = new SideMenuBusiness().GetSideMenus(SessionUtility.GetBasicUserInfo().UserCode, RoleCode.OrdinaryUser);
            string result = JsonConvert.SerializeObject(treeNodes);
            return Content(result);
        }
    }
}
